package com.woniu.intercepter;

import com.woniu.constant.SystemConstant;
import com.woniu.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthIntercepter implements HandlerInterceptor {
     @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
            String uri=request.getRequestURI();
            if (uri.endsWith("login.html")||
               uri.endsWith("login")||
               uri.endsWith("doLogin")||
               uri.endsWith(".css")||
               uri.endsWith(".js")||
               uri.endsWith(".gif")||
               uri.endsWith(".png")
            ){
                return true;
            }
         HttpSession session=request.getSession();
         System.out.println("经过拦截器");
         User user1=(User)session.getAttribute(SystemConstant.CURRENT_USER);
         if (user1==null){
             response.sendRedirect("login");
             return false;
         }
         return true;
    }
}
